/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.kz.shange.imgSvc;

import java.awt.Image;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 *
 * @author Administrator
 */
public abstract class MediaUtil {

    public static InputStream getMediaStream(String url) throws IOException {
        HttpClient httpclient = new DefaultHttpClient();
        try {
            HttpGet httpget = new HttpGet(url);
            httpget.setHeader("User-Agent", "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
            HttpResponse response = httpclient.execute(httpget);

// 得到网络资源的字节数组,并写入文件
            HttpEntity entity = response.getEntity();
            if (entity != null) {   
                
                Image img = ImageIO.read(entity.getContent());
                
                return entity.getContent();
            }
        } finally {
            httpclient.getConnectionManager().shutdown();
        }
        return null;
    }

    public static void download(String url, String filePathName) {
        HttpClient httpclient = new DefaultHttpClient();
        try {
            HttpGet httpget = new HttpGet(url);
            httpget.setHeader("User-Agent", "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
            HttpResponse response = httpclient.execute(httpget);

            File storeFile = new File(filePathName);
            FileOutputStream output = new FileOutputStream(storeFile);

// 得到网络资源的字节数组,并写入文件
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                try {
                    byte b[] = new byte[1024];
                    int j = 0;
                    while ((j = instream.read(b)) != -1) {                        
                        output.write(b, 0, j);
                    }
                    output.flush();
                    output.close();
                } catch (IOException ex) {
// In case of an IOException the connection will be released
// back to the connection manager automatically
                    throw ex;
                } catch (RuntimeException ex) {
// In case of an unexpected exception you may want to abort
// the HTTP request in order to shut down the underlying
// connection immediately.
                    httpget.abort();
                    throw ex;
                } finally {
// Closing the input stream will trigger connection release
                    try {
                        instream.close();
                    } catch (IOException ignore) {
                    }
                }
            }

        } catch (IOException e) {
            log(e.getMessage());
        } catch (RuntimeException e) {
            log(e.getMessage());
        } finally {
            httpclient.getConnectionManager().shutdown();
        }
    }

    private static void log(String msg) {
        System.out.println(msg);
    }
}
